package com.action.book;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.controller.Action;
import com.dao.BookDao;
import com.dao.RentalDao;
import com.domain.Book;

public class BookInsertProAction implements Action{
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("euc-kr");
		
		HttpSession session = request.getSession();
		String mem_id = (String)session.getAttribute("mem_id");
		if(!mem_id.equals("admin")){
			return "/view/member/loginForm.jsp";
		}
		
		int rowSize = Integer.parseInt(request.getParameter("size"));
		List<Book> books = new ArrayList<Book>();
		Book book = null;
		
		for(int i=0; i<rowSize-1; i++){
			
			String book_name = request.getParameter("book_name_"+i);
			String author = request.getParameter("author_"+i);
			String publisher = request.getParameter("publisher_"+i);
			String summary = request.getParameter("summary_"+i);
			
			book = new Book();
			book.setBook_name(book_name);
			book.setAuthor(author);
			book.setPublisher(publisher);
			book.setSummary(summary);
			
			books.add(book);
		}
		
		BookDao dao = BookDao.getinstance();
		for(Book b:books){
			dao.insertBook(b);
			Book rentBook = dao.getBookByName(b.getBook_name());
			int book_num = rentBook.getBook_num();
			
			RentalDao rentalDao = RentalDao.getInstance();
			rentalDao.insertRental(book_num);
		}
		
		return "/view/member/adminChooseForm.jsp";
	}
}
